#! /usr/bin/env python
from ftplib import FTP
import datetime

# global varibles
hostname= ''
port    = ''
user    = ''
passwd  = ''
filename= ''
urlsw   = 0
i       = 0
to_write= []
path    = '/'
timeout = 0

def write_doc(aurl):
    global i, urlsw, to_write, path
    if len(aurl)>1 and aurl[0]=='/':
        path = aurl[0:-1]
    elif len(aurl)>urlsw and (aurl[urlsw:]!='.') and \
       (aurl[urlsw:]!='..') and aurl[0]=='-':
        to_write.append('ftp://'+user+':'+passwd+'@'+ \
                        hostname+':'+port+ \
                        path+aurl[urlsw:]+"\r\n")
        i = i + 1
    
if __name__ == "__main__":
    fp = open('config.txt', "r")
    while True:
        line = fp.readline()
        if line == "":
            # end
            break
        # parse line
        line = line.strip()
        if line == "":
            # empty line
            continue
        if line.startswith("#"):
            # comments
            continue
        (name, sep, value) = line.partition("=")
        if sep == "=":
            name = name.strip().lower()
            value = value.strip()
            if name == "hostname":
                hostname = value
            elif name == "port":
                port = value
            elif name == "user":
                user = value
            elif name == "passwd":
                passwd = value
            elif name == "filename":
                filename = value
            elif name == "urlsw":
                urlsw = int(value)
            elif name == "timeout":
                timeout = int(value)
    fp.close()
    ftp = FTP()
    ftp.connect(hostname, port)
    ftp.login(user, passwd)
    print("Loading...\r\n")
    ftp.retrlines('LIST -alR', write_doc)
    large_str = ''.join(to_write)
    print("Retrieved "+str(i)+" entries.\r\n")
    if i>0:
        open(filename, 'w').write('')
        open(filename, 'a').write(large_str)
    print(str(datetime.datetime.now())+"\r\nOK!\r\n")
    ftp.quit()
